package com.himalayan.bear.controller;

import com.himalayan.bear.util.FileUtil;
import com.himalayan.bear.util.RespUtil;
import com.himalayan.bear.util.ValidCodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FormController extends BaseController {
    /**
     * 跳转至登陆页面
     *
     * @return
     */
    @GetMapping("/toLogin")
    public String login() {
        return "page/login/login";
    }
    /**
     * 跳转至注册页面
     *
     * @return
     */
    @GetMapping("/toRegister")
    public String register() {
        return "page/login/register";
    }

    @GetMapping({"/", "/index"})
    public String index() {
        return "page/index";
    }

    /**
     * 获取验证码
     * @param request
     * @param response
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/validCode")
    public void validCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
        BufferedImage bufImg = ValidCodeUtil.drawValidCode(request, response, 95, 40);
        ImageIO.write(bufImg, "jpg", response.getOutputStream());
    }

    /**
     * 遍历当前的文件列表
     *
     * @param fileName
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/admin/fileList")
    public String fileList(String fileName, HttpServletRequest request, Model model) {
        String path;
        String[] fileNames;
        if (StringUtils.isEmpty(fileName)) {
            path = request.getServletContext().getRealPath("/files/");
        } else {
            path = request.getServletContext().getRealPath("/files/" + fileName);
            fileNames = fileName.split("/+");
            model.addAttribute("fileNames", fileNames);
            String[] upnames = new String[fileNames.length - 1];
            System.arraycopy(fileNames, 0, upnames, 0, fileNames.length - 1);
            model.addAttribute("upName", RespUtil.array2String("/", upnames));
        }
        File localFile = new File(path);
        if (localFile.exists()) {
            File[] files = localFile.listFiles();
            if (files == null || files.length == 0) {
                model.addAttribute("message", "无数据");
                return "page/fileList";
            }
            List<Map<String, Object>> listFiles = new ArrayList<>();
            for (File file : files) {
                Map<String, Object> mapFile = new HashMap<>();
                if (file.isDirectory()) {
                    mapFile.put("flag", 0); // 是个目录
                } else {
                    mapFile.put("flag", 1); // 文件
                }
                mapFile.put("fileName", file.getName());
                listFiles.add(mapFile);
            }
            model.addAttribute("files", listFiles);
            model.addAttribute("fileName", fileName);
        }
        return "page/fileList";
    }
}
